-- attack_stealing
-- create by panyl
-- 技能：银之手（怪盗基德主动技）
-- 目前支持如下配置：
-- attack_stealing(slimeSkill, baseRate)

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 目前仅支持盗取怪物的
        if target.type ~= OBJECT_TYPE_MONSTER and target.type ~= OBJECT_TYPE_GHOST_SHIP then
            assert(false);
        end

        local prop;
        local level;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 抛出银之手发动事件；免疫了该技能时也要播放
        EventMgr.fire(event.ATTACK_STEALING, { ["pos"] = target:getPos(), });

        -- 免疫冈布奥主动技
        prop = PropM.combine(target, "slime_skill_immunity", 1);
        if prop[3] > 0 then
            SkillM.getSequence(target):immunity(source, target, skillId);
            return;
        end

        -- 先做伤害
        local damage = source:queryAttrib("attack");
        CombatM.doDamage(source, target, damage, skillId);

        local package = MonsterM.query(target.classId, "package") or {};
        local normal = package["normal"];
        local better = package["better"];

        -- 如果该怪物已经被盗取过了
        if target.dbase:query("be_stolen") == 1 then
            return;
        end

        -- 没有东西可以盗取
        if not normal and not better then
            return;
        end

        local item = normal;
        local rand = DungeonM.getRandSeed("attack_stealing") % 1000;
        local rate = para[2] * level;
        if better and rand < rate then
            -- 高级的
            item = better;
        end

        -- 没有物品？
        if not item then
            return;
        end

        -- 如果有多个，就随机一个
        if type(item[1]) == "table" then
            rand = DungeonM.getRandSeed("attack_stealing") % #item;
            table.sort(item, function(a, b) return a[2] * 1000 + a[3] < b[2] * 1000 + b[3] end);
            item = item[rand + 1];
        end

        -- 标记为被盗过了
        target.dbase:set("be_stolen", 1);

        -- 不再掉落
        local grid = DungeonM.getGridByPos(target:getPos());
        local keep = true;
        if grid.type ~= GRID_TYPE_BOX and grid.type ~= GRID_TYPE_COFFIN and type(package["stolen_still_drop"]) ~= "number" then
            grid.bonus = nil;
            keep = false;
        end

        -- 奖励就行了
        BonusM.doBonus(item, "attack_stealing");

        -- 抛出被偷窃事件
        EventMgr.fire(event.BE_STOLEN, {
            ["pos"]   = target:getPos(),
            ["type"]  = GRID_TYPE_MONSTER,
            ["bonus"] = { ["bonus"] = { item } },
            ["remainGrid"] = true,
            ["keep"]  = keep,
            -- ["cost"] = eventCost,
        });

        -- 抛出事件尝试完成：竞技场获得对手物品
        EventMgr.fire(event.GET_ARENA_ENEMY_ITEM, { ["pos"] = target:getPos(), ["bonus"] = item, });

        -- 如果有被盗后变身的，那么变身下
        if package["stolen_transform"] then
            MonsterM.dungeonMonsterTransform(target, package["stolen_transform"], { ["layer"] = target.dbase:query("layer")}, true);

            -- 抛出变身事件
            EventMgr.fire(event.MONSTER_TRANSFORM, {["pos"] = target:getPos(), ["class_id"] = package["stolen_transform"], });
        end

        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = prop[3];
        local rate = math.modf(para[2] * level / 10);
        desc = string.gsub(desc, "{damage}", source:queryAttrib("attack"));

        desc = string.gsub(desc, "{rate}", rate);

        return desc;
    end,
};
